﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using ElectronicSelling.BaseEntity;
using ElectronicSelling.DataAccess;
using System.Data.SqlClient;

namespace ElectronicSelling.BusinessLogic
{
    public class HomeAccountLogic
    {
        public static DataTable GetUserLogin(User user)
        {
            return HomeAccountAccess.GetUserLogin(user);
        }

        public static bool IsAuthentication(User user)
        {
            DataTable dt = GetUserLogin(user);
            if (dt.Rows.Count == 1)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Kiểm tra tồn tại
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool IsExistCustomer(User user)
        {
            DataTable dt = HomeAccountAccess.GetExistCustomer(user);
            if (dt.Rows.Count == 1)
            {
                return true;
            }
            return false;
        }

        public static bool IsInsertCustomer(User user)
        {
            return HomeAccountAccess.InsertCustomer(user);
        }


        /// <summary>
        /// lấy thông tin chi tiết khách hàng
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static DataTable GetCustomerDetail(User user)
        {
            return HomeAccountAccess.GetCustomerDetail(user);
        }

        /// <summary>
        /// lấy chi tiết khách hàng 
        /// </summary>
        /// <param name="user"></param>
        /// <returns>object</returns>
        public static User GetCusTableToObject(User user)
        {
            DataTable dt = GetCustomerDetail(user);
            if (dt.Rows.Count == 1)
            {
                DataRow dr = dt.Rows[0];
                User usr = new User();
                usr.UserId = int.Parse(dr["UserId"].ToString());
                usr.UserName = dr["UserName"].ToString();
                usr.Phone = dr["Phone"].ToString();
                usr.Gender = dr["Gender"].ToString();
                usr.FullName = dr["FullName"].ToString();
                usr.DateOfBirth = dr["DateOfBirth"].ToString();
                usr.Email = dr["Email"].ToString();
                usr.Address = dr["Address"].ToString();
                return usr;
            }
            return null;
        }

        public static bool IsUpdateCustomerForOrder(User user)
        {
            return HomeAccountAccess.UpdateCustomerForOrder(user);
        }

        public static DataTable GetPassWordById(User user)
        {
            return HomeAccountAccess.GetPassWordById(user);
        }

        /// <summary>
        /// Kiểm tra pass cũ có trùng
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool IsNotValidOldPassWord(User user)
        {
            DataTable dt = GetPassWordById(user);
            DataRow dr = dt.Rows[0];
            user.Password = dr["Password"].ToString();
            if (user.Password == Library.GennerateToMD5(user.OldPassword))
                return false;
            return true;
        }

        /// <summary>
        /// change pass
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool IsUpdatePassword(User user)
        {
            user.Password = Library.GennerateToMD5(user.Password);
            return HomeAccountAccess.IsUpdatePassword(user);
        }

        public static bool IsUpdateCustomer(User user)
        {
            return HomeAccountAccess.IsUpdateCustomer(user);
        }
    }
}